You can sort arrays by key with ksort() and krsort().